package org.jxht.web.api;

import org.jxht.dto.entity.ResponseCourseEntity;
import org.jxht.dto.entity.ResponseScheduleEntity;
import org.jxht.dto.CourseDto;
import org.jxht.service.CourseService;
import org.jxht.util.GsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * Created by huang on 2017/3/20 0020.
 */
@Controller
@RequestMapping("JxhtWeb/CourseController")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @RequestMapping(value = "/{requestId}/addCourseInfo")
    public String addCourseInfo(@PathVariable(value = "requestId")int requestId,
                                @RequestParam(value = "requestJson")String requestJson){
        List<CourseDto> dtos = GsonUtil.parseJsonArrayWithGson(requestJson,CourseDto.class);
        for(CourseDto dto:dtos){
            courseService.insertCourseService(dto);
        }
        return "SUCCESS";
    }

    @RequestMapping(value = "/{requestId}/changeCourseInfo")
    public String changeCourseInfo(@PathVariable(value = "requestId")int requestId,
                                   @RequestParam(value = "requestJson")String requestJson){
        List<CourseDto> dtos = GsonUtil.parseJsonArrayWithGson(requestJson,CourseDto.class);
        for(CourseDto dto:dtos){
            courseService.updataCourseService(dto);
        }
        return "SUCCESS";
    }

    @RequestMapping(value = "/{requestId}/deleteCourseInfo")
    public String deleteCourseInfo(@PathVariable(value = "requestId")int requestId){
        courseService.deleteCourseService(new CourseDto(requestId));
        return "SUCCESS";
    }

    /**
     * 根据Id获取课程信息
     * @param requestId 请求Id
     * @param requestWay 请求方式
     */
    @RequestMapping(value = "/{requestId}/GetCourseById",
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<ResponseCourseEntity> getCourseById(@PathVariable(value = "requestId")int requestId,
                                                    @RequestParam(value = "requestWay")String requestWay){
        ResponseCourseEntity entity = new ResponseCourseEntity();
        if ("stuId".equals(requestWay)){
            entity.setCouId(requestId);
        }else if ("teaId".equals(requestWay)){
            entity.setTeaId(requestId);
        }
        else {
            entity.setMajId(Integer.toString(requestId));
        }
        List<ResponseCourseEntity> lists = courseService.queryCourseDetail(entity);
        return lists;
    }

    /**
     * 获取所有课程信息
     * @return List<ResponseCourseEntity>
     * @throws IOException
     */
    @RequestMapping(value = "/GetCourseAll",method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<ResponseCourseEntity> getCourseAll()throws IOException{
        ResponseCourseEntity entity = new ResponseCourseEntity();
        List<ResponseCourseEntity> lists = courseService.queryCourseDetail(entity);
        return lists;
    }

    /**
     * 获取公共选修课程
     */
    @RequestMapping(value = "{requestId}/getElectiveCourse",method = RequestMethod.GET)
    @ResponseBody
    public List<ResponseCourseEntity> getElectiveCourse(@PathVariable(value = "requestId")int requestId) throws IOException {
//        @PathVariable(value = "requestId")int requestId
        if(0 != courseService.queryaElectiveCourse()) {
            ResponseCourseEntity entity = new ResponseCourseEntity();
            entity.setCouProperty("公共选修");
            List<ResponseCourseEntity> lists = courseService.queryCourseDetail(entity);
            return lists;
        }else {
            return null;
        }
    }

    /**
     * 获取课程表
     */
    @RequestMapping(value = "{requestId}/getSchedule",method = RequestMethod.GET,
            produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public List<ResponseScheduleEntity> getSchedule(@PathVariable("requestId")long requestId,
                                                    @RequestParam("requestWay")String requestWay){
        return courseService.queryStuSchedule(requestId,requestWay);
    }
}
